package com.hao.proxy.crawler;

import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hao.proxy.context.Constants;
import com.hao.proxy.model.Proxy;
import com.hao.proxy.utils.Sleep;


public abstract class AbstractCrawler implements Runnable{
	
	protected final static Logger logger = LoggerFactory.getLogger(AbstractCrawler.class);
	
	protected abstract Set<Proxy> getProxy();
	
	@Override
	public void run() {
		while(true){
			if(Constants.FILTER_QUEUE.size()<100 && Constants.PROXY_QUEUE.remainingCapacity()>0){
				Set<Proxy> set = getProxy();
				logger.debug(this.getClass().getName()+"获取到"+set.size()+"个待过滤代理");
				logger.debug("当前待过滤代理数："+Constants.FILTER_QUEUE.size());
				if(Constants.FILTER_QUEUE.size()==0){
					Constants.FILTER_QUEUE.addAll(set);
				}else{
					for (Proxy proxy : set) {
						if(Constants.FILTER_QUEUE.contains(proxy)||Constants.PROXY_QUEUE.contains(proxy)){
							continue;
						}
						Constants.FILTER_QUEUE.offer(proxy);
					}
				}
			}
			Sleep.sleep(1000*60);
		}
	}
}
